<template>
  <div>
    <!-- 查询工具栏 -->
    <el-form :inline="true" :model="dataForm">
      <el-form-item label="卡号：">
        <el-input v-model="dataForm.cardNo" placeholder="卡号" readonly></el-input>
      </el-form-item>
      <el-form-item label="时间：">
        <el-date-picker
            v-model="dateTimeRange"
            end-placeholder="结束日期"
            range-separator="至"
            start-placeholder="开始日期"
            type="datetimerange">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button circle icon="el-icon-search" type="primary" @click="getDataList"></el-button>
      </el-form-item>
    </el-form>
    <!-- 数据列表 -->
    <el-table :data="dataList" border size="small" stripe style="width: 100%">
      <el-table-column align="center" label="#" type="index" width="50"></el-table-column>
      <el-table-column label="卡号" prop="cardNo" width="130">
        <template slot-scope="scope">
          <el-tag effect="light" type="success">{{ scope.row.cardNo }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="充值金额" prop="amount"></el-table-column>
      <el-table-column label="支付金额" prop="payAmount"></el-table-column>
      <el-table-column label="赠送金额" prop="giftAmount"></el-table-column>
      <el-table-column label="充值后余额" prop="balanceAfter"></el-table-column>
      <el-table-column label="充值油站" prop="rechargeStationName"></el-table-column>
      <el-table-column label="充值时间" prop="rechargeDate" show-overflow-tooltip></el-table-column>
      <el-table-column label="支付方式" prop="payTypeName"></el-table-column>
      <el-table-column label="支付状态" prop="payStatusName"></el-table-column>
      <el-table-column label="开卡油站" prop="cardStationName"></el-table-column>
      <el-table-column align="left" label="操作" width="120">
        <template slot-scope="scope">
          <el-tooltip content="打印小票" placement="top">
            <el-button circle icon="el-icon-printer" size="mini" type="success"
                       @click="handleTicket(scope.row)"></el-button>
          </el-tooltip>
          <el-tooltip v-show="scope.row.payStatus == 1" content="发起退款" placement="top">
            <el-button circle icon="el-icon-delete" size="mini" type="danger"
                       @click="handleRefund(scope.row)"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination :current-page="param.pageNum" :page-size="param.pageSize" :page-sizes="[10,20,30,50]"
                     :total="total" background layout="total, sizes, prev, pager, next, jumper"
                     @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
    </div>
  </div>
</template>
<script>
import moment from 'moment'
import {getCardRechargeList, rechargeRefund} from '@/api/card'
import {mapGetters} from 'vuex'
import {getCardRechargeReceiptText} from '@/utils/receipt'

export default {
  name: 'CardRechargeInfo',
  props: ['cardInfo'],
  data () {
    return {
      total: 0,
      param: {
        pageNum: 1,
        pageSize: 10,
        dataObj: {
          cardNo: '',
          rechargeDate1: '',
          rechargeDate2: ''
        }
      },
      dateTimeRange: [],
      dataForm: {
        masterUid: '',
        cardNo: '',
        startDate: '',
        endDate: ''
      },
      dataList: []
    }
  },
  computed: {
    ...mapGetters(['stationId', 'operator', 'shiftName'])
  },
  mounted () {
    this.dateTimeRange.push(moment().subtract(7, 'days').startOf('day'))
    this.dateTimeRange.push(moment().endOf('day'))
  },
  methods: {
    getDataList () {
      if (this.dataForm.masterUid) {
        this.$message.warning('副卡没有充值记录！')
        return false
      }
      if (!this.dataForm.cardNo) {
        this.$message.warning('卡号不能为空！')
        return false
      }
      this.param.dataObj.cardNo = this.dataForm.cardNo
      this.param.dataObj.rechargeDate1 = this.dateTimeRange[0]
      this.param.dataObj.rechargeDate2 = this.dateTimeRange[1]
      getCardRechargeList(this.param).then(res => {
        console.log(res)
        this.dataList = res.data.records
        this.total = res.data.total
      }).catch(err => {
        console.log(err)
        this.$message.error(err || '查询失败！')
      })
    },
    handleSizeChange (size) {
      this.param.pageSize = size
      this.getDataList()
    },
    handleCurrentChange (page) {
      this.param.pageNum = page
      this.getDataList()
    },
    handleRefund (rowData) {
      console.log('handleRefund')
      console.log(rowData)
      this.$confirm('是否退款' + rowData.amount + '元', '是否确定退款？', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        dangerouslyUseHTMLString: true
      }).then(() => {
        rowData.operator = this.operator
        rechargeRefund(rowData).then(res => {
          console.log(res)
          this.$message.success(res.msg || '退款成功')
          this.getDataList()
        }).catch(err => {
          console.log(err)
          this.$message.warning(err || '退款失败')
        })
      }).catch(err => {
        console.log(err)
      })
    },
    handleTicket (rowData) {
      console.log('handleTicket')
      console.log(rowData)
      // 打印充值小票
      var receipt = {
        stationName: rowData.rechargeStationName,
        cardNo: rowData.cardNo,
        paymentTypeName: rowData.payTypeName,
        amount: rowData.amount,
        balance: rowData.balanceAfter,
        operator: this.operator,
        shiftName: this.shiftName,
        rechargeDate: rowData.rechargeDate
      }
      console.log(receipt)
      this.$store.dispatch('addToReceiptList', getCardRechargeReceiptText(receipt))
    }
  },
  watch: {
    cardInfo (newVal, oldVal) {
      console.log(newVal)
      this.dataForm.cardNo = newVal.cardNo
      this.dataForm.masterUid = newVal.masterUid
      if (!newVal.cardNo) {
        this.dataList = []
        this.total = 0
      }
    }
  }
}
</script>

<style scoped>
.pagination-container {
  margin-top: 10px;
}
</style>
